<template>
  <div class="home-container">
    <van-nav-bar class="page-nav-bar" fixed>
      <template v-slot:title>
        <van-button
          class="search-btn"
          type="info"
          size="small"
          round
          icon="search"
          to="/search"
          >搜索</van-button
        >
      </template>
    </van-nav-bar>
    <van-tabs v-model="active" animated swipeable>
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <articleList :channel="item" />
      </van-tab>

      <template v-slot:nav-right>
        <div class="placeholder"></div>
        <div class="hanbao-btn" @click="up">
          <i class="iconfont icongengduo "></i>
        </div>
      </template>
    </van-tabs>
    <van-popup
      v-model="show"
      closeable
      position="bottom"
      :style="{ height: '100%' }"
      close-icon-position="top-left"
      ><channelEdit
        :channelItem="channels"
        :active="active"
        @changeActive="changeActive($event)"
        @clearChannel="clearCh"
      >
      </channelEdit
    ></van-popup>
  </div>
</template>
<script>
import { getUserChennals } from '@/api/user.js'
import { deleteUserChannel } from '@/api/channel'
import articleList from '@/components/home/article-list'
import channelEdit from '@/components/home/channel-edit'
import { setItem, getItem } from '@/utils/storage.js'
import { mapState } from 'vuex'
export default {
  name: 'list',
  created() {
    this.loadChannels()
  },
  data() {
    return {
      active: 0,
      show: false,
      channels: []
    }
  },
  computed: { ...mapState(['user']) },
  methods: {
    async clearCh(item, index) {
      this.channels.splice(
        this.channels.findIndex(ele => {
          return ele.id === item.id
        }),
        1
      )
      if (index < this.active) this.active--
      if (this.user) {
        try {
          // 已登录，数据存储到线上
          await deleteUserChannel(item.id)
          this.$toast('删除成功')
        } catch (err) {
          console.log(err)
          this.$toast('删除失败')
        }
      } else {
        setItem('channels', this.channels)
      }
    },
    changeActive(index) {
      this.active = index
      this.show = false
    },
    up() {
      this.show = true
    },
    async loadChannels() {
      try {
        if (this.user) {
          const { data } = await getUserChennals()
          this.channels = data.data.channels
        } else {
          const channels = getItem('channels')
          if (channels) {
            this.channels = channels
          } else {
            const { data } = await getUserChennals()
            this.channels = data.data.channels
          }
        }
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    }
  },
  components: {
    articleList,
    channelEdit
  }
}
</script>
<style lang="less" scoped>
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/ .van-tabs__wrap {
    position: fixed;
    top: 92px;
    z-index: 2;
    width: 100%;
  }
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }

  /deep/ .van-tabs {
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      height: 80px;
      .van-tab__text {
        font-size: 27px;
        color: #777777;
      }
    }

    .van-tab--active .van-tab__text {
      color: #333 !important;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background: rgba(50, 150, 250, 1);
      border-radius: 3px;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding: 0;
    }
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  .hanbao-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    .iconfont {
      font-size: 33px;
    }
    &:before {
      content: '';
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>
